import json

# 姓名  年龄  学号
class Student:
    def __init__(self):
        with open('student.txt', "r", encoding='utf-8') as s:
            self.all_stu = json.loads(s.read())
        self.choose()

    def choose(self):
        while True:
            print("""
                *************************
                *    1: 添加学生        *
                *    2: 删除学生        *
                *    3: 修改学生        *
                *    4: 查询学生信息    *
                *    5: 查询所有学生信息*
                *    6: 保存信息        *
                *    7: 退出系统        *
                *************************
                    """)
            stu_cho = input("Please choose the number you want")
            if stu_cho == str(1):
                self.add_stu()
            elif stu_cho == str(2):
                self.del_stu()
            elif stu_cho == str(3):
                self.cor_stu()
            elif stu_cho == str(4):
                self.fin_stu()
            elif stu_cho == str(5):
                self.fin_all()
            elif stu_cho == str(6):
                self.stu_sav()
            elif stu_cho == str(7):
                break

    def add_stu(self):
        stu_info = {}
        name = input("what's 油 name?")
        stu_info['name'] = name
        age = input("好 old are 油?")
        stu_info['age'] = age
        num = input("what's 油 number?")
        stu_info['num'] = num
        self.all_stu.append(stu_info)

    def del_stu(self):
        del_1 = input("which num of the 死毒dent do 油 want to delete?")
        for i in range(len(self.all_stu)):
            if self.all_stu[i]['num'] == del_1:
                print(f"{self.all_stu[i]['name']}已被删除")
                del self.all_stu[i]
                break
        else:
            print(f'该学生不存在')

    def cor_stu(self):
        cor_1 = input("which num of the 死毒dent do 油 want to correct?")
        for i in range(len(self.all_stu)):
            if self.all_stu[i]['num'] == cor_1:
                name = input('please 抠瓦克特 your name？')
                age = input('please 抠瓦克特 your age？')
                self.all_stu[i].update({
                    "name": name,
                    "age": age
                })
                print(f"学号{cor_1} 信息修改为姓名{name}, 年龄{age}")
                break
        else:
            print(f'该学生不存在')

    def fin_stu(self):
        fin_1 = input("which num of the 死毒dent do 油 want to 发爱的?")
        for item in self.all_stu:
            if item['num'] == fin_1:
                print(f"姓名:{item['name']} 年龄:{item['age']} 学号:{item['num']}")

    def fin_all(self):
        for item in self.all_stu:
            print(f"姓名:{item['name']} 年龄:{item['age']} 学号:{item['num']}")

    def stu_sav(self):
        with open('student.txt', "w", encoding='utf-8') as s:
            s.write(json.dumps(self.all_stu))
            
s = Student()
